/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.jhelper.unit05.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class ExceptionDemo {

    /*
    public static void main(String[] args) {        
        
       try{
           System.out.println(calculate(9, 0));          
       }catch(Exception e){
           System.err.println("发生了异常:"+e.toString());
//           e.printStackTrace();
       }
        
        
    }
     * 
     */
    public static void main(String[] args) throws Exception {
        try {            
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("请输入第一个数， 被除数：");
            int num1 = Integer.parseInt(br.readLine());
            System.out.println("请输入第二个数， 除数：");
            int num2 = Integer.parseInt(br.readLine());
            int result;  
           
            result = calculate(num1,num2);            
            System.out.println("结果是："+result);
            
        } catch (NumberFormatException e) {
            System.err.println("发生了异常,必须输入数字字符串");
        } catch(ArithmeticException e){
            System.err.println("发生了异常，除数不能为0");
        } catch(IOException e){
            System.err.println("发生了IO异常。");
        } finally{
                
        }
    }
    
    
    public static int calculate(int a, int b) throws Exception {
        if(b==0){
            throw new Exception();
        }else{
            return a/b;
        }
        
        
    }
}
